home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 6 code / TCP / finger / finger.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-03  |  4.0 KB  |  194 lines  |  [TEXT/MPS ]

  1. /* MacTCP finger client            */
  2. /* written by Steven Falkenburg    */
  3. /*                                 */
  4. /* 10/5/90    original coding        */
  5.  
  6. #include "compat.h"
  7.  
  8. #ifdef PROTOS
  9. #include <Types.h>
  10. #include <Memory.h>
  11. #include <Packages.h>
  12. #include <CursorCtl.h>
  13. #endif
  14.  
  15. #include <String.h>
  16.  
  17. #include "CvtAddr.h"
  18. #include "MacTCPCommonTypes.h"
  19. #include "TCPPB.h"
  20. #include "TCPHi.h"
  21.  
  22.  
  23. /* constants */
  24.  
  25. #define kFingerPort 79        /* TCP port assigned for finger protocol */
  26. #define kBufSize    16384    /* Size for TCP stream buffer and receive buffer */
  27. #define kTimeOut    20        /* Timeout for TCP commands */
  28.  
  29.  
  30. /* fuction prototypes */
  31.  
  32. void main(int argc,char *argv[]);
  33. OSErr Finger(char *userid,char *hostName,Handle *fingerData);
  34. OSErr GetFingerData(unsigned long stream,Handle *fingerData);
  35. void FixCRLF(char *data);
  36. Boolean GiveTime(short sleepTime);
  37.  
  38. /* globals */
  39.  
  40. Boolean gCancel = false;    /* this is set to true if the user cancels an operation */
  41.  
  42. /*    main entry point for finger        */
  43. /*                                     */
  44. /*    usage: finger <user>@<host>        */
  45. /*                                    */
  46. /*    this function parses the args from the command line,        */
  47. /*    calls Finger() to get info and prints the returned info.    */
  48.  
  49. void main(int argc,char *argv[])
  50. {
  51.     OSErr err;
  52.     Handle theFinger;
  53.     char userid[256],host[256];
  54.     
  55.     if (argc != 2) {
  56.         printf("Wrong number of parameters to finger call\n");
  57.         return;
  58.     }
  59.     
  60.     sscanf(argv[1],"%[^@]@%s",userid,host);
  61.     
  62.     strcat(userid,"\n\r");
  63.     
  64.     err = Finger(userid,host,&theFinger);
  65.     
  66.     if (err == noErr) {
  67.         HLock(theFinger);
  68.         FixCRLF(*theFinger);
  69.         printf("\n%s\n",*theFinger);
  70.         DisposHandle(theFinger);
  71.     }
  72.     else
  73.         printf("An error has occurred: %hd\n",err);
  74. }
  75.  
  76.  
  77. /*    Finger()                                                        */
  78. /*                                                                     */
  79. /*    This function converts the host    string to an IP number, opens a    */
  80. /*    connection to the remote host on TCP port 79, sends the id to    */
  81. /*    the remote host and waits for the information on the receiving    */
  82. /*    stream.  After this    information is sent, the connection is        */
  83. /*    is closed down.                                                    */
  84.  
  85. OSErr Finger(char *userid,char *hostName,Handle *fingerData)
  86. {
  87.     OSErr err;
  88.     unsigned long ipAddress;
  89.     unsigned long stream;
  90.     
  91.     /* open the network driver */
  92.     
  93.     err = InitNetwork();
  94.     if (err!=noErr)
  95.         return err;
  96.     
  97.     /* get remote machine's network number */
  98.     
  99.     err = ConvertStringToAddr(hostName,&ipAddress);
  100.     if (err!=noErr)
  101.         return err;
  102.     
  103.     /* open a TCP stream */
  104.     
  105.     err = CreateStream(&stream,kBufSize);
  106.     if (err!=noErr)
  107.         return err;
  108.     
  109.     err = OpenConnection(stream,ipAddress,kFingerPort,kTimeOut);
  110.     if (err==noErr) {
  111.         err = SendData(stream,userid,(unsigned short)strlen(userid),false);
  112.         if (err==noErr)
  113.             err = GetFingerData(stream,fingerData);
  114.         CloseConnection(stream);
  115.     }
  116.     
  117.     ReleaseStream(stream);
  118.     return err;
  119. }
  120.  
  121.  
  122. OSErr GetFingerData(unsigned long stream,Handle *fingerData)
  123. {
  124.     OSErr err;
  125.     long bufOffset = 0;
  126.     unsigned short dataLength;
  127.     Ptr data;
  128.     
  129.     *fingerData = NewHandle(kBufSize);
  130.     err = MemError();
  131.     if (err!=noErr)
  132.         return err;
  133.         
  134.     HLock(*fingerData);
  135.     data = **fingerData;
  136.     dataLength = kBufSize;
  137.     
  138.     do {
  139.         err = RecvData(stream,data,&dataLength,false);
  140.         if (err==noErr) {
  141.             bufOffset += dataLength;
  142.             dataLength = kBufSize;
  143.             HUnlock(*fingerData);
  144.             SetHandleSize(*fingerData,bufOffset+kBufSize);
  145.             err = MemError();
  146.             HLock(*fingerData);
  147.             data = **fingerData + bufOffset;
  148.         }
  149.     } while (err==noErr);
  150.     
  151.     data[0] = '\0';
  152.     
  153.     HUnlock(*fingerData);
  154.     if (err == connectionClosing)
  155.         err = noErr;
  156. }
  157.  
  158.  
  159. /* FixCRLF() removes the linefeeds from a    */
  160. /* text buffer.  This is necessary, since    */
  161. /* all text on the network is embedded with    */
  162. /* carraige return linefeed pairs.            */
  163.  
  164. void FixCRLF(char *data)
  165. {
  166.     register char *source,*dest;
  167.     long length;
  168.     
  169.     length = strlen(data);
  170.     
  171.     if (*data) {
  172.         source = dest = data;
  173.         while ((source - data) < (length-1)) {
  174.             if (*source == '\r')
  175.                 source++;
  176.             *dest++ = *source++;
  177.         }
  178.         if (*source != '\r' && (source - data) < length)
  179.             *dest++ = *source++;
  180.         length = dest - data;
  181.     }
  182.     
  183.     *dest = '\0';
  184. }
  185.  
  186.  
  187. /* this routine would normally be a callback for giving time to background
  188.     apps */
  189.     
  190. Boolean GiveTime(short sleepTime)
  191. {
  192.     SpinCursor(1);
  193.     return true;
  194. }